package edu.spbu.eclipse.gmftests.a_zhukova.model.diagram.edit.policies;

import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.gmf.runtime.notation.View;
import edu.spbu.eclipse.gmftests.a_zhukova.model.Feature;
import edu.spbu.eclipse.gmftests.a_zhukova.model.ModelPackage;

import edu.spbu.eclipse.gmftests.a_zhukova.model.diagram.providers.MapElementTypes;

import org.eclipse.gef.commands.UnexecutableCommand;

import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand;

import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;

/**
 * @generated
 */
public class FeatureItemSemanticEditPolicy extends
		MapBaseItemSemanticEditPolicy {

	/**
	 * @generated
	 */
	protected Command getDestroyElementCommand(DestroyElementRequest req) {
		return getMSLWrapper(new DestroyElementCommand(req) {

			protected EObject getElementToDestroy() {
				View view = (View) getHost().getModel();
				EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
				if (annotation != null) {
					return view;
				}
				return super.getElementToDestroy();
			}

		});
	}

	/**
	 * @generated
	 */
	protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) {
		if (MapElementTypes.FeatureFeature_connexions_3001 == req
				.getElementType()) {
			return req.getTarget() == null ? getCreateStartOutgoingFeature_Feature_connexions3001Command(req)
					: getCreateCompleteIncomingFeature_Feature_connexions3001Command(req);
		}
		return super.getCreateRelationshipCommand(req);
	}

	/**
	 * @generated
	 */
	protected Command getCreateStartOutgoingFeature_Feature_connexions3001Command(
			CreateRelationshipRequest req) {

		return new Command() {
		};
	}

	/**
	 * @generated
	 */
	protected Command getCreateCompleteIncomingFeature_Feature_connexions3001Command(
			CreateRelationshipRequest req) {
		if (!(req.getSource() instanceof Feature)) {
			return UnexecutableCommand.INSTANCE;
		}
		Feature element = (Feature) req.getSource();
		if (element.getFeature_connexions().contains(req.getTarget())) {
			return UnexecutableCommand.INSTANCE;
		}
		SetRequest setReq = new SetRequest(req.getSource(),
				ModelPackage.eINSTANCE.getFeature_Feature_connexions(), req
						.getTarget());
		return getMSLWrapper(new SetValueCommand(setReq));
	}
}
